var ZAWModel = {

    getSpriteFrames_nameArr:function(a,callback){
        var arr = [];
        for(var i=0;i<a.count;++i){
            cc.loader.loadRes(a[i], cc.SpriteFrame, function (err, spriteFrame) { 
                arr.push(spriteFrame);
                if(arr.count == a.count)
                {
                    callback(arr);
                }
            });
        }
    },
   
    /* 炸弹动画 */
    startBomb:function(father,callback){
        cc.loader.loadRes('/prefab/bomb',function(error,res){

            father.schedule(function(){
                var obs = cc.instantiate(res);
                obs.x = cc.view.getVisibleSize().width/-2;
                obs.y = this.getRandomNumber(250,550);
                obs.getComponent(cc.RigidBody).linearVelocity = cc.v2(160, 20);
                obs.runAction(cc.repeatForever(cc.rotateBy(1, 120)));
                father.zawLayer.addChild(obs);


                var obs2 = cc.instantiate(res);
                obs2.x = cc.view.getVisibleSize().width/2;
                obs2.y = this.getRandomNumber(250,550);
                obs2.getComponent(cc.RigidBody).linearVelocity = cc.v2(-160, 20);
                obs2.runAction(cc.repeatForever(cc.rotateBy(1, -120)));
                father.zawLayer.addChild(obs2);
            }.bind(father),1,4)
        
            father.schedule(function(){ callback(); },10,0)
        }.bind(this));
    },

    /*飞镖 */
    startFeiBiao:function(father,callback){
        cc.loader.loadRes('/prefab/feibiao',function(error,res){
            var arr = [cc.v2(0, 400),cc.v2(70,500),cc.v2(-70,500),cc.v2(0, 600),cc.v2(140,600),cc.v2(-140,600),cc.v2(-70,700),cc.v2(70, 700),cc.v2(-210,700),cc.v2(210,700)];
            for(var i=0;i<arr.length;++i){
                var obs = cc.instantiate(res);
                obs.position = arr[i];
                father.zawLayer.addChild(obs);
            }
            father.schedule(function(){ callback(); },5,0)
        }.bind(this));
    },

    /* 飞机掉炸弹 */
    startPlane:function(father,callback){
        cc.loader.loadRes("/game/lv3/plane", cc.SpriteFrame, function (err, spriteFrame) {  
            var plane = new cc.Node('plane');
            const sprite = plane.addComponent(cc.Sprite);
            sprite.spriteFrame = spriteFrame;
            plane.position = cc.v2(400,450);    
            plane.setScale(0.6, 0.6);
            plane.rotation = -10;
            father.zawLayer.addChild(plane);
            plane.runAction(cc.rotateTo(2, 10));
            var seq = cc.sequence(cc.bezierTo(2, [cc.v2(400, 500), cc.v2(-150, 250), cc.v2(-600, 500)]), cc.removeSelf());
            plane.runAction(seq);
            father.schedule(function(){
                cc.loader.loadRes('/prefab/p_bomb',function(error,res){
                    var p = plane.getPosition();
                    var bomb = cc.instantiate(res);
                    bomb.x = p.x;
                    bomb.y = p.y - 60;
                    father.zawLayer.addChild(bomb);
                })
            }.bind(father),0.15,10);


            father.schedule(function(){ callback(); },5,0)
        });
    },

    /*  静态的足球和篮球 */
    startStaticBall:function(father,callback){
        cc.loader.loadResArray(['/prefab/basketball','/prefab/football'],function(err,resArr){
            var arr = [{obj:resArr[0],position:cc.v2(-180,400)},{obj:resArr[1],position:cc.v2(-60,400)},{obj:resArr[1],position:cc.v2(60,400)},{obj:resArr[0],position:cc.v2(180,400)},
                        {obj:resArr[1],position:cc.v2(-180,500)},{obj:resArr[0],position:cc.v2(-60,500)},{obj:resArr[1],position:cc.v2(60,500)},{obj:resArr[0],position:cc.v2(180,500)},
                        {obj:resArr[0],position:cc.v2(-180,600)},{obj:resArr[1],position:cc.v2(-60,600)},{obj:resArr[0],position:cc.v2(60,600)},{obj:resArr[1],position:cc.v2(180,600)},
                        {obj:resArr[1],position:cc.v2(-180,700)},{obj:resArr[0],position:cc.v2(-60,700)},{obj:resArr[0],position:cc.v2(60,700)},{obj:resArr[1],position:cc.v2(180,700)},
            ];
            for(var i=0;i<arr.length;i++){
                var ov = arr[i];
                var obs = cc.instantiate(ov.obj);
                obs.position = ov.position;
                father.zawLayer.addChild(obs);
            }
            father.schedule(function(){ callback(); },5,0)
        });
    },

    /* 动态的足球 */
    startDynamicBall:function(father,callback){
        cc.loader.loadResArray(['/prefab/basketball','/prefab/football'],function(err,resArr){
            father.schedule(function(){
                var b = Math.floor(Math.random()*10000)%2;
                 var f = b==0?1:0;  
                var obs = cc.instantiate(resArr[b]);
                obs.x = cc.view.getVisibleSize().width/-2;
                obs.y = this.getRandomNumber(350,550);
                obs.getComponent(cc.RigidBody).linearVelocity = cc.v2(160, 20);
                obs.runAction(cc.repeatForever(cc.rotateBy(1, 120)));
                father.zawLayer.addChild(obs);

                var obs2 = cc.instantiate(resArr[f]);
                obs2.x = cc.view.getVisibleSize().width/2;
                obs2.y = this.getRandomNumber(350,550);
                obs2.getComponent(cc.RigidBody).linearVelocity = cc.v2(-160, 20);
                obs2.runAction(cc.repeatForever(cc.rotateBy(1, -120)));
                father.zawLayer.addChild(obs2);
            }.bind(father),1,4)
        });
    },

    /* 推❤心动画 */
    startTuiXing:function(father,callback){
        cc.loader.loadResArray(['/prefab/tui_rw','/prefab/t_xing'],function(err,resArr){
            var t_rw = cc.instantiate(resArr[0]);
            t_rw.position = cc.v2(450,400); 
            father.zawLayer.addChild(t_rw);
            var seq = cc.sequence(cc.moveTo(1, cc.v2(200, 400)), cc.callFunc(function () {
                var animScript = t_rw.getComponent('tui_xin');
                animScript.start_animation(function(){
                    console.log("动画播放完成");
                    var xin = cc.instantiate(resArr[1]);
                    xin.position = cc.v2(120,400);
                    father.zawLayer.addChild(xin);
                });
            }))
            t_rw.runAction(seq);
            father.schedule(function(){ callback(); },5,0)
        });
    },

    /*获取从a~b之间的一个随机数*/
    getRandomNumber:function(a,b){
        return Math.floor(Math.random()*10000)%(Math.abs(b-a))+a;
     },
}
module.exports = ZAWModel;